package lium.lbs.lbsim.fragments

import android.content.Intent
import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import cn.jpush.im.android.api.JMessageClient
import com.buz.hjcdriver.bean.GetUserInfoResultBean
import com.lmlibrary.Constants
import lium.lbs.lbsim.utils.UserUtils
import com.lmlibrary.bean.ResponseBean
import lium.lbs.lbsim.utils.callbck.JsonCallback
import com.lzy.okgo.model.Response
import kotlinx.android.synthetic.main.fragment_my.*
import lium.lbs.lbsim.R
import lium.lbs.lbsim.activity.*
import lium.lbs.lbsim.jgchat.utils.photovideo.takevideo.utils.LogUtils
import lium.lbs.lbsim.utils.SpUtils
import java.util.HashMap

class MyFragment : BaseFragment() {

    override fun getLayoutId(): Int {
        return R.layout.fragment_my
    }

    companion object {
        fun newInstance(): MyFragment {
            return MyFragment()
        }
    }

    override fun initView() {
        addOnClickListeners(
            R.id.my_wallet,
            R.id.my_set,
            R.id.changename,
            R.id.headImage,
            R.id.change_signature,
            R.id.tv_loginout
        )
    }

    override fun onClick(v: View?) {
        when (v!!.id) {
            R.id.my_wallet -> {
                val intent = Intent(activity, WalletActivity::class.java)
                startActivity(intent)
            }
            R.id.my_set -> {
                val intent = Intent(activity, SetActivity::class.java)
                startActivity(intent)
            }
            R.id.changename -> {
                val intent = Intent(activity, ChangeNameHeadActivity::class.java)
                startActivity(intent)
            }
            R.id.headImage -> {
                val intent = Intent(activity, ChangeNameHeadActivity::class.java)
                startActivity(intent)
            }
            R.id.change_signature -> {
                val intent = Intent(activity, ChangeSignatureActivity::class.java)
                startActivity(intent)
            }
            R.id.tv_loginout -> {
                JMessageClient.logout()
                val intent = Intent(activity, lium.lbs.lbsim.activity.LoginActivity::class.java)
                SpUtils.getInstance().put(Constants.isLogin, false)
                //intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent)
                activity?.finish()

            }

        }
    }

    override fun onResume() {
        super.onResume()
        getmyinfo()
    }

    private fun getmyinfo() {
        //隐示获取个人信息
        val param = HashMap<String, String>()
        postData(
            "/member/getMemberInfo",
            param,
            object : JsonCallback<ResponseBean<GetUserInfoResultBean>>(baseActivity) {
                override fun onSuccess(response: Response<ResponseBean<GetUserInfoResultBean>>) {
                    UserUtils.login(response.body().data.memberId + "")
                    UserUtils.setUserData(
                        response.body().data.uniqueCode,
                        response.body().data.nickName,
                        response.body().data.headImage,
                        response.body().data.phone
                    )
                    if (response.body().data.isVerified.equals("0")){
                        SpUtils.getInstance().put(Constants.isVerified, false)
                    } else{
                        SpUtils.getInstance().put(Constants.isVerified, true)
                    }


                    displayImage(response.body().data.headImage, R.drawable.icon_defalut_head, headImage)
                    uniqueCode.text = response.body().data.uniqueCode
                    nickName.text = response.body().data.nickName
                    signature.text = response.body().data.signature
                }
            })
    }

}